package com.veal98.tree;

import com.veal98.bean.TreeNode;

import java.util.LinkedList;
import java.util.Map;
import java.util.Queue;

public class MaximumDepthOfBinaryTree {


    public int maxDepth(TreeNode root) {
        if (root == null) {
            return 0;
        }

        Queue<TreeNode> queue = new LinkedList<>();
        // 根节点入队
        queue.offer(root);
        // 树的深度
        int depth = 0;

        while (!queue.isEmpty()) {
            // 当前层节点的个数
            int size = queue.size();
            depth ++;
            for (int i = 0;  i < size;  i++) {
                TreeNode cur = queue.poll();
                if (cur.left != null) {
                    queue.offer(cur.left);
                }
                if (cur.right != null) {
                    queue.offer(cur.right);
                }
            }
        }

        return depth;
    }

    public int maxDepth2(TreeNode root) {
        // 递归出口
        if (root == null) {
            return 0;
        }
        if (root.left == null && root.right == null) {
            return 1;
        }

        // 左子树的高度
        int leftDepth = maxDepth2(root.left);
        // 右子树的高度
        int rightDepth = maxDepth2(root.right);

        return Math.max(leftDepth, rightDepth) + 1;
    }

}
